local longdan = fk.CreateSkill({
  name = "mini_mou__longdan",
})

Fk:loadTranslationTable{
  ["mini_mou__longdan"] = "龙胆",
  [":mini_mou__longdan"] = "你可以将一张【杀】当【闪】、【闪】当【杀】使用或打出，然后若你本回合未造成过伤害，你摸一张牌。<br>"..
  "⬤　二级：你可以将一张基本牌当任意本回合未以此法转化过的基本牌使用或打出，然后你摸一张牌。",

  ["#mini_mou__longdan0"] = "龙胆：将一张【杀】当【闪】、【闪】当【杀】使用或打出",
  ["#mini_mou__longdan1"] = "龙胆：将一张基本牌当任意基本牌使用或打出",

  ["$mini_mou__longdan1"] = "要想前进，先过我这关！",
  ["$mini_mou__longdan2"] = "龙威萦绕，虎胆长存。",
}

longdan:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic",
  prompt = function (self, player)
    return "#mini_mou__longdan" .. player:getMark("mini_mou__jizhu")
  end,
  interaction = function(self, player)
    local jizhuMark = player:getMark("mini_mou__jizhu")
    local all_names = jizhuMark == 0 and { "slash", "jink" } or Fk:getAllCardNames("b")
    local names = player:getViewAsCardNames(longdan.name, all_names, nil, jizhuMark ~= 0 and player:getTableMark("mini_mou__longdan-turn") or nil)
    if #names == 0 then return end
    return UI.CardNameBox { choices = names, all_choices = all_names }
  end,
  filter_pattern = function (self, player, card_name)
    if player:getMark("mini_mou__jizhu") == 0 then
      local pat = {
        max_num = 1,
        min_num = 1,
        pattern = "slash,jink",
      }
      if card_name == "slash" then
        pat.pattern = "jink"
      elseif card_name == "jink" then
        pat.pattern = "slash"
      end
      return pat
    else
      return {
        max_num = 1,
        min_num = 1,
        pattern = ".|.|.|.|.|basic",
      }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected ~= 0 or not self.interaction.data then return false end
    local card = Fk:getCardById(to_select)
    if player:getMark("mini_mou__jizhu") == 0 then
      return card.trueName == (self.interaction.data == "jink" and "slash" or "jink")
    else
      return card.type == Card.TypeBasic
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = longdan.name
    return card
  end,
  before_use = function (self, player, use)
    if player:getMark("mini_mou__jizhu") > 0 then
      player.room:addTableMark(player, "mini_mou__longdan-turn", use.card.trueName)
    end
  end,
  after_use = function (self, player, use)
    if not player.dead then
      if player:getMark("mini_mou__jizhu") == 0 then
        if #player.room.logic:getActualDamageEvents(1, function(e)
          return e.data.from == player
        end, Player.HistoryTurn) == 0 then
          player:drawCards(1, longdan.name)
        end
      else
        player:drawCards(1, longdan.name)
      end
    end
  end,
  enabled_at_response = function (self, player, response)
    if Fk.currentResponsePattern then
      local names = { "slash", "jink" }
      if player:getMark("mini_mou__jizhu") > 0 then
        names = table.filter(Fk:getAllCardNames("b"), function (name)
          return not table.contains(player:getTableMark("mini_mou__longdan-turn"), Fk:cloneCard(name).trueName)
        end)
      end
      return table.find(names, function (name)
        local card = Fk:cloneCard(name)
        card.skillName = longdan.name
        return Exppattern:Parse(Fk.currentResponsePattern):match(card)
      end)
    end
  end,
})

longdan:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "mini_mou__longdan-turn", 0)
  player.room:setPlayerMark(player, "mini_mou__jizhu", 0)
end)

return longdan
